/**
 * Created with IntelliJ IDEA
 * Description:
 * User: Administrator
 * Data: 2023 - 08 - 22
 * Time: 15:40
 */
//牛客 链表中的节点每k个一组翻转
class Solution4 {
    public ListNode reverseKGroup (ListNode head, int k) {
        ListNode cur1 = head;
        for(int i = 0; i < k; i++) {
            if(cur1 == null) return head;//if语句要写在cur1=cur1.next前面
            cur1 = cur1.next;
        }
        ListNode pre = null;
        ListNode cur2 = head;
        while(cur2 != cur1) {//cur1是下一组的头节点
            ListNode curNext = cur2.next;
            cur2.next = pre;
            pre = cur2;
            cur2 = curNext;
        }
        ListNode node = reverseKGroup(cur1, k);//cur1是下一组的头节点
        head.next = node;//翻转后头结点head变为了尾节点
        return pre;//pre变为了新的头节点
    }
}
